/// 心率平均值数据模型
class HeartRateAverageData {
  final int average;
  final String? dateStart;
  final String? dateEnd;
  final String recordDate;
  final String weekName;
  final String? endTime;
  final int maxValue;
  final int minValue;
  final List<HeartRateInfo> manualInfoVOS;
  final List<HeartRateInfo> automaticInfoVOS;

  HeartRateAverageData({
    required this.average,
    this.dateStart,
    this.dateEnd,
    required this.recordDate,
    required this.weekName,
    this.endTime,
    required this.maxValue,
    required this.minValue,
    required this.manualInfoVOS,
    required this.automaticInfoVOS,
  });

  factory HeartRateAverageData.fromJson(Map<String, dynamic> json) {
    return HeartRateAverageData(
      average: json['average'] ?? 0,
      dateStart: json['dateStart'],
      dateEnd: json['dateEnd'],
      recordDate: json['recordDate'] ?? '',
      weekName: json['weekName'] ?? '',
      endTime: json['endTime'],
      maxValue: json['maxValue'] ?? 0,
      minValue: json['minValue'] ?? 0,
      manualInfoVOS: (json['manualInfoVOS'] as List<dynamic>?)
              ?.map((item) => HeartRateInfo.fromJson(item))
              .toList() ??
          [],
      automaticInfoVOS: (json['automaticInfoVOS'] as List<dynamic>?)
              ?.map((item) => HeartRateInfo.fromJson(item))
              .toList() ??
          [],
    );
  }
}

/// 心率信息
class HeartRateInfo {
  final String code;
  final int value;
  final int? number;
  final int? maxValue;
  final int? minValue;

  HeartRateInfo({
    required this.code,
    required this.value,
    this.number,
    this.maxValue,
    this.minValue,
  });

  factory HeartRateInfo.fromJson(Map<String, dynamic> json) {
    // Handle value as both String and int
    int parsedValue = 0;
    if (json['value'] != null) {
      if (json['value'] is String) {
        parsedValue = int.tryParse(json['value']) ?? 0;
      } else if (json['value'] is int) {
        parsedValue = json['value'];
      }
    }

    return HeartRateInfo(
      code: json['code'] ?? '',
      value: parsedValue,
      number: json['number'],
      maxValue: json['maxValue'],
      minValue: json['minValue'],
    );
  }
}

/// 平均睡眠心率数据模型
class SleepHeartRateData {
  final int average;
  final String averageInfo;
  final int averageValue;
  final int minHeartRate;
  final String minHeartRateInfo;
  final int minHeartRateValue;
  final int fluctuationRange;
  final String fluctuationRangeInfo;
  final int fluctuationRangeValue;

  SleepHeartRateData({
    required this.average,
    required this.averageInfo,
    required this.averageValue,
    required this.minHeartRate,
    required this.minHeartRateInfo,
    required this.minHeartRateValue,
    required this.fluctuationRange,
    required this.fluctuationRangeInfo,
    required this.fluctuationRangeValue,
  });

  factory SleepHeartRateData.fromJson(Map<String, dynamic> json) {
    return SleepHeartRateData(
      average: json['average'] ?? 0,
      averageInfo: json['averageInfo'] ?? '',
      averageValue: json['averageValue'] ?? 0,
      minHeartRate: json['minHeartRate'] ?? 0,
      minHeartRateInfo: json['minHeartRateInfo'] ?? '',
      minHeartRateValue: json['minHeartRateValue'] ?? 0,
      fluctuationRange: json['fluctuationRange'] ?? 0,
      fluctuationRangeInfo: json['fluctuationRangeInfo'] ?? '',
      fluctuationRangeValue: json['fluctuationRangeValue'] ?? 0,
    );
  }

  /// 判断平均心率是否正常
  bool get isAverageNormal => averageInfo == '优秀' || averageInfo == '良好';

  /// 判断最低心率是否正常
  bool get isMinHeartRateNormal =>
      minHeartRateInfo == '优秀' || minHeartRateInfo == '良好';

  /// 判断波动幅度是否正常
  bool get isFluctuationNormal =>
      fluctuationRangeInfo == '优秀' || fluctuationRangeInfo == '良好';
}

/// 运动期间心率数据模型
class SportHeartRateData {
  final String totalDuration;
  final int warmUpRelaxRate;
  final String warmUpRelaxTime;
  final int fatBurningRate;
  final String fatBurningTime;
  final int heartLungsRate;
  final String heartLungsTime;
  final int enduranceReinforcementRate;
  final String enduranceReinforcementTime;
  final int anaerobicLimitRate;
  final String anaerobicLimitTime;

  SportHeartRateData({
    required this.totalDuration,
    required this.warmUpRelaxRate,
    required this.warmUpRelaxTime,
    required this.fatBurningRate,
    required this.fatBurningTime,
    required this.heartLungsRate,
    required this.heartLungsTime,
    required this.enduranceReinforcementRate,
    required this.enduranceReinforcementTime,
    required this.anaerobicLimitRate,
    required this.anaerobicLimitTime,
  });

  factory SportHeartRateData.fromJson(Map<String, dynamic> json) {
    return SportHeartRateData(
      totalDuration: json['totalDuration'] ?? '0小时0分钟',
      warmUpRelaxRate: json['warmUpRelaxRate'] ?? 0,
      warmUpRelaxTime: json['warmUpRelaxTime'] ?? '',
      fatBurningRate: json['fatBurningRate'] ?? 0,
      fatBurningTime: json['fatBurningTime'] ?? '',
      heartLungsRate: json['heartLungsRate'] ?? 0,
      heartLungsTime: json['heartLungsTime'] ?? '',
      enduranceReinforcementRate: json['enduranceReinforcementRate'] ?? 0,
      enduranceReinforcementTime: json['enduranceReinforcementTime'] ?? '',
      anaerobicLimitRate: json['anaerobicLimitRate'] ?? 0,
      anaerobicLimitTime: json['anaerobicLimitTime'] ?? '',
    );
  }

  /// 获取所有区间的时间列表
  List<String> get allZoneTimes => [
        warmUpRelaxTime,
        fatBurningTime,
        heartLungsTime,
        enduranceReinforcementTime,
        anaerobicLimitTime,
      ];

  /// 获取所有区间的比率列表
  List<int> get allZoneRates => [
        warmUpRelaxRate,
        fatBurningRate,
        heartLungsRate,
        enduranceReinforcementRate,
        anaerobicLimitRate,
      ];
}

/// 心率变异性平均值数据模型
class HrvAverageData {
  final int average;
  final String? dateStart;
  final String? dateEnd;
  final String recordDate;
  final String weekName;
  final String? endTime;
  final int maxValue;
  final int minValue;
  final List<HeartRateInfo> manualInfoVOS;
  final List<HeartRateInfo> automaticInfoVOS;

  HrvAverageData({
    required this.average,
    this.dateStart,
    this.dateEnd,
    required this.recordDate,
    required this.weekName,
    this.endTime,
    required this.maxValue,
    required this.minValue,
    required this.manualInfoVOS,
    required this.automaticInfoVOS,
  });

  factory HrvAverageData.fromJson(Map<String, dynamic> json) {
    return HrvAverageData(
      average: json['average'] ?? 0,
      dateStart: json['dateStart'],
      dateEnd: json['dateEnd'],
      recordDate: json['recordDate'] ?? '',
      weekName: json['weekName'] ?? '',
      endTime: json['endTime'],
      maxValue: json['maxValue'] ?? 0,
      minValue: json['minValue'] ?? 0,
      manualInfoVOS: (json['manualInfoVOS'] as List<dynamic>?)
              ?.map((item) => HeartRateInfo.fromJson(item))
              .toList() ??
          [],
      automaticInfoVOS: (json['automaticInfoVOS'] as List<dynamic>?)
              ?.map((item) => HeartRateInfo.fromJson(item))
              .toList() ??
          [],
    );
  }
}
